package com.example.xd.defaultcall;

import android.content.Intent;
import android.telecom.Call;
import android.telecom.InCallService;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import static com.example.xd.defaultcall.Constance.CALL_BR_URL;
import static com.example.xd.defaultcall.Constance.RECEIVE_BR_URL;

/**
 * @auther XuDan on 2020/5/29.
 */
public class PhoneCallService extends InCallService {

    private Call.Callback callback = new Call.Callback() {
        @Override
        public void onStateChanged(Call call, int state) {
            super.onStateChanged(call, state);
            switch (state) {
                case Call.STATE_NEW:
                    Log.i("xd---", "STATE_NEW");
                    break;
                case Call.STATE_DIALING:
                    Log.i("xd---", "STATE_DIALING");//拨打电话,响铃时状态
                    break;
                case Call.STATE_RINGING:
                    Log.i("xd---", "STATE_RINGING");
                    break;
                case Call.STATE_HOLDING:
                    Log.i("xd---", "STATE_HOLDING");
                    break;
                case Call.STATE_ACTIVE://拨打电话,对方接通时的回调   来电时,我接通电话
                    if (CallType.CALL_IN.equals(callType)) {
                        Intent intent = new Intent(RECEIVE_BR_URL);
                        intent.putExtra("state", "STATE_ACTIVE");
                        sendBroadcast(intent);
                    }
                    if (CallType.CALL_OUT.equals(callType)) {
                        Intent intent = new Intent(CALL_BR_URL);
                        intent.putExtra("state", "STATE_ACTIVE");
                        sendBroadcast(intent);
                    }
                    Log.i("xd---", "STATE_ACTIVE");
                    break;
                case Call.STATE_DISCONNECTED://拨打电话,对方挂断    来电时,对方挂断电话
                    if (CallType.CALL_IN.equals(callType)) {
                        Intent intent = new Intent(RECEIVE_BR_URL);
                        intent.putExtra("state", "STATE_DISCONNECTED");
                        sendBroadcast(intent);
                    }
                    if (CallType.CALL_OUT.equals(callType)) {
                        Intent intent = new Intent(CALL_BR_URL);
                        intent.putExtra("state", "STATE_DISCONNECTED");
                        sendBroadcast(intent);
                    }
                    Log.i("xd---", "STATE_DISCONNECTED");
                    break;
                case Call.STATE_SELECT_PHONE_ACCOUNT:
                    Log.i("xd---", "STATE_SELECT_PHONE_ACCOUNT");
                    break;
                case Call.STATE_CONNECTING:
                    Log.i("xd---", "STATE_CONNECTING");
                    break;
                case Call.STATE_PULLING_CALL:
                    Log.i("xd---", "STATE_PULLING_CALL");
                    break;
                case Call.STATE_DISCONNECTING://拨打电话,自己挂断      来电时,我挂断电话
                    if (CallType.CALL_IN.equals(callType)) {
                        Intent intent = new Intent(RECEIVE_BR_URL);
                        intent.putExtra("state", "STATE_DISCONNECTING");
                        sendBroadcast(intent);
                    }
                    if (CallType.CALL_OUT.equals(callType)) {
                        Intent intent = new Intent(CALL_BR_URL);
                        intent.putExtra("state", "STATE_DISCONNECTING");
                        sendBroadcast(intent);
                    }
                    Log.i("xd---", "STATE_DISCONNECTING");
                    break;
            }
        }
    };
    private int mState;
    CallType callType = null;

    @Override
    public void onCallAdded(Call call) {
        super.onCallAdded(call);
        call.registerCallback(callback);
        PhoneCallManager.call = call;
        mState = call.getState();
        switch (mState) {
            case Call.STATE_RINGING:
                callType = CallType.CALL_IN;
                break;
            case Call.STATE_CONNECTING:
                callType = CallType.CALL_OUT;
                break;
        }

        if (callType != null) {
            if (callType == CallType.CALL_IN) {
                Call.Details details = call.getDetails();
                String phoneNum = details.getHandle().toString().substring(4).replaceAll("%20", "");
                Intent intent = new Intent(PhoneCallService.this, UIActivity.class);
                intent.putExtra("tel", phoneNum);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
            if (callType == CallType.CALL_OUT) {
                Call.Details details = call.getDetails();
                String phoneNum = details.getHandle().toString().substring(4).replaceAll("%20", "");
                Intent intent = new Intent(PhoneCallService.this, CallUIActivity.class);
                intent.putExtra("tel", phoneNum);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        }


    }

    @Override
    public void onCallRemoved(Call call) {
        super.onCallRemoved(call);
        call.unregisterCallback(callback);
        PhoneCallManager.call = null;
        MyApp application = (MyApp) getApplication();
        AppCompatActivity uiActivity = application.getActivity();
        uiActivity.finish();
    }
}


